package com.ldzl.controller.outbound;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ldzl.dto.AddOutboundDTO;
import com.ldzl.pojo.CkProductSales;
import com.ldzl.pojo.CkProductSalesLine;
import com.ldzl.pojo.ScSalesOrder;
import com.ldzl.pojo.ScSalesOrderDetail;
import com.ldzl.service.CkProductSalesLineService;
import com.ldzl.service.CkProductSalesService;
import com.ldzl.service.ScSalesOrderDetailService;
import com.ldzl.service.ScSalesOrderService;
import com.ruoyi.common.core.domain.UR;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 出库控制请
 */
@RestController
@RequestMapping("/outbound")
public class OutboundController extends BaseController {

    @Autowired
    private CkProductSalesService pss; // 出库服务
    @Autowired
    private CkProductSalesLineService psls; // 出库行服务
    @Autowired
    private ScSalesOrderService sos; // 销售订单服务
    @Autowired
    private ScSalesOrderDetailService sods; // 销售订单行服务

    /**
     * 查询待质检的出库单
     * @param sales_code
     * @param sales_name
     * @return
     */
    @GetMapping("/selectCkProductSales/{sales_code}/{sales_name}")
    public TableDataInfo selectCkProductSales(@PathVariable("sales_code") String sales_code,
                                              @PathVariable("sales_name") String sales_name){
        startPage();
        return getDataTable(pss.selectCkProductSales(sales_code, sales_name));
    }

    /**
     * 查询指定出库单下的商品详情 质检使用
     * @param sales_id
     * @return
     */
    @GetMapping("/ckProductSalesLine_qc/{sales_id}")
    public TableDataInfo selectCkProductSalesLine_qc(@PathVariable("sales_id") Long sales_id){
        return getDataTable(psls.selectCkProductSalesLine(sales_id));
    }

    /**
     * 查询指定出库单下的商品详情
     * @param sales_id
     * @return
     */
    @GetMapping("/ckProductSalesLine/{sales_id}")
    public TableDataInfo selectCkProductSalesLine(@PathVariable("sales_id") Long sales_id){
        //startPage();
        return getDataTable(psls.selectCkProductSalesLine(sales_id));
    }


    /**
     * 查询指定出库单下的商品详情 (更新后)
     * @param salesLine
     * @return
     */
    @PostMapping("/selectSalesLine")
    public TableDataInfo selectSalesLine(CkProductSalesLine salesLine){
        startPage();
        return getDataTable(psls.list(new QueryWrapper<CkProductSalesLine>()
                .eq("sales_id",salesLine.getSales_id())
                .eq("is_delete",0)));
    }

    /**
     * 修改出库单状态
     * @param sales_id
     * @param status
     * @return
     */
    @GetMapping("/ckProductSalesStatus/{sales_id}/{status}")
    public UR updateCkProductSalesStatusStatus(@PathVariable("sales_id") Long sales_id,
                                               @PathVariable("status") String status){
        if(pss.updateCkProductSalesStatusStatus(status,sales_id))
            return UR.ok("修改成功");
        else
            return UR.fail("修改失败");
    }

    /**
     * 查询销售出库单
     * @param sales
     * @return
     */
    @PostMapping("/findOutbound")
    public TableDataInfo findOutbound(CkProductSales sales){
        startPage();
        return getDataTable(pss.selectProduct_Sales(sales));
    }

    /**
     * 查询已确认的销售订单
     * @param salesOrder
     * @return
     */
    @PostMapping("/selectSalesOrder")
    public TableDataInfo selectSalesOrder(ScSalesOrder salesOrder){
        startPage();
        return getDataTable(sos.selectSalesOrder(salesOrder));
    }

    /**
     * 查询指定销售订单下的商品信息
     * @param orderDetail
     * @return
     */
    @PostMapping("/selectSalesOrderDetail")
    public TableDataInfo selectSalesOrderDetail(ScSalesOrderDetail orderDetail){
        startPage();
        return getDataTable(sods.selectSalesOrderDetail(orderDetail));
    }

    /**
     * 添加销售出库单
     * @param salesDto
     * @return
     */
    @PostMapping("/addOutbound")
    public UR addOutbound(@RequestBody AddOutboundDTO  salesDto){
        int isSuccess = pss.insertProduct_Sales(salesDto);
        if(isSuccess == 1)
            return UR.ok("添加成功");
        else if(isSuccess == 4)
            return UR.fail("销售订单名称已存在");
        else
            return UR.fail("添加失败");
    }

    /**
     * 销售出库
     * @param sales_id
     * @return
     */
    @GetMapping("/warehouseOutbound/{sales_id}/{update_by}")
    public UR warehouseOutbound(@PathVariable("sales_id") Long sales_id,
                                 @PathVariable("update_by") String update_by){
        if(pss.warehouseOutbound(sales_id,update_by))
            return UR.ok("出库成功");
        else
            return UR.fail("出库失败");
    }


    /**
     * 删除销售订单
     * @param sales_id
     * @return
     */
    @GetMapping("/updateSales_delete/{sales_id}/{update_by}")
    public UR updateSales_delete(@PathVariable("sales_id") Long sales_id,
                                 @PathVariable("update_by") String update_by){
        if(pss.updateSales_delete(sales_id,update_by))
            return UR.ok("删除成功");
        else
            return UR.fail("删除失败");
    }

    /**
     * 批量删除销售订单
     * @param list_sales_id
     * @param update_by
     * @return
     */
    @PostMapping("/deleteBatch/{update_by}")
    public UR deleteBatch(@RequestBody List<Long> list_sales_id,
                           @PathVariable("update_by") String update_by){
        if(pss.deleteBatch(list_sales_id,update_by))
            return UR.ok("删除成功");
        else
            return UR.fail("删除失败");
    }

}
